    mui.init({});
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var currentPage=[1,1,1,1];
    var pageSize=10;
    var total_page=[0,0,0,0];
    var total_count=[0,0,0,0];
    var num_arr=[500,1000,1500,2000];
    var sale_trade_id=0;
    var country='中国';
    var bank={};
    
    window.onload = function(){
    }
    
    //阻尼系数
	var deceleration = mui.os.ios ? 0.003 : 0.0009;
	mui('.mui-scroll-wrapper').scroll({
		bounce: false,
		indicators: true, //是否显示滚动条
		deceleration: deceleration
	});
	
    mui.plusReady(function() {
		//关闭等待框
		plus.nativeUI.closeWaiting();
		//显示当前页面
		mui.currentWebview.show();
	
		refresh_data();
		
		refresh_bank();
		
		var width=document.documentElement.clientWidth;
    	var height=document.documentElement.clientHeight;
    	document.getElementById('account_pwd').style.width=width+'px';
    	document.getElementById('account_pwd').style.height=height+'px';
    	
    	//点中国
		document.getElementById('country1').addEventListener('tap', function() {
			  //打开区域选择
			  mui.openWindow({
			    id:'area_select.html',
			    url:'/pages/transaction/area_select.html',
			    extras:{
			         selected_country: document.getElementById('country1').innerHTML, //自定义扩展参数，可以用来处理页面间传值
			         type: 4
			    },
			    show: {
					aniShow: 'pop-in'
				},
				waiting: {
					autoShow: false
				}
			  });
		}, false);
		
		
    	//出售
    	mui(".mui-table-view").on('tap','.mui-table-view-cell .list_r .sale_btn',function(){
    		if(!bank.bank_card || !bank.bank_name){
    			mui.alert('请去添加银行卡');
    			//打开银行卡
				mui.openWindow({
					id: 'addcard.html',
					url: '/pages/setting/addcard.html',
					extras: {
						bank_id: 0//自定义扩展参数，可以用来处理页面间传值
					},
					show: {
						autoShow: false
					},
					waiting: {
						autoShow: true,
						title: '正在加载...'
					}
				});
    			return;
    		}
    		
    		sale_trade_id=this.getAttribute('data-tradeId');
    		document.getElementById('account_pwd').style.display="block";
			document.getElementById('main_content').style.display="block";
			document.getElementById('keyboard').style.display="block";
			var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
			mui.each(pwd_array, function(index, item) {
				if(item.value != '') {
					item.value = '';
				}
			});
			
    	});
    	
    	document.getElementById('close_win').addEventListener('tap', function() {
			document.getElementById('account_pwd').style.display="none";
			document.getElementById('main_content').style.display="none";
			document.getElementById('keyboard').style.display="none";
		})
    	
    	    //点击数字
			mui('#keyboard').on('tap', '.num.data_num', function() {
				var number=this.getAttribute('data-num');
				var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
				var f=false;
				var pwd='';
				mui.each(pwd_array,function(index,item){
				   if(item.value==''){
				   	   item.value=number;
				   	   return false;
				   }
				}) ;

				mui.each(pwd_array,function(index,item){
				   if(item.value==''){
				   	   f=true;
				   	   return false;
				   }
				   pwd+=item.value;
				}) ;
				
				if(!f){
					//mui.alert('正在后台处理卖出...');
					var Info={
						user_id:userinfo.user_id,
						trade_pwd: pwd,
						type: 2,
						tid: sale_trade_id,
						bank_name: bank.bank_name,
						bank_addr: bank.bank_addr,
						bank_account: bank.bank_account,
						bank_card: bank.bank_card,
						_token: userinfo.token
					}
					document.getElementById('account_pwd').style.display="none";
					document.getElementById('main_content').style.display="none";
					document.getElementById('keyboard').style.display="none";
					plus.nativeUI.showWaiting();
					mui.ajax({
						url: apiurl + "Trade/updateTrade/",
						data: Info,
						async: true,
						dataType: 'json',
						crossDomain: true, //强制使用5+跨域
						type: 'post',
						timeout: 10000,
						success: function(res) {
							console.log(JSON.stringify(res));
							plus.nativeUI.closeWaiting();
							if(res.status == -3) {
								mui.toast(res.msg);
								app.out();
								return false;
							}
							
							// 请求成功
							if(res.status < 1) {
								mui.alert(res.msg);
							} else {
								mui.toast('出售成功');
								userinfo.a1_money=res.data.a1_money;
								userinfo.a2_money=res.data.a2_money;
								app.setState(userinfo);
						        mui.fire(plus.webview.getLaunchWebview(), 'refresh_amoney');
						        //打开卖出订单详情
								mui.openWindow({
								    id:'buyorder.html',
								    url:'buyorder.html',
								    extras:{
								      trade_id:sale_trade_id,//自定义扩展参数，可以用来处理页面间传值
								      froms:'sellout_center'
								    },
								    show: {
										autoShow:false
									},
									waiting:{
								       autoShow:true,
								       title:'正在加载...'
								   }
								});
								
							}
						},
						error: function(xhr, type, errorThrown) {
							//console.log(JSON.stringify(xhr));
							//console.log(JSON.stringify(type));
							//console.log(JSON.stringify(errorThrown));
							mui.toast('操作失败！');
						}
					});
				}
			})
			
			
			//点击删除
			mui('#keyboard').on('tap', '#numC', function() {
			     var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
				 var len=pwd_array.length;
				 for(i=len-1;i>=0;i--){
				 	if(pwd_array[i].value!=''){
				 		pwd_array[i].value='';
				 		break;
				 	}
				 }
			});
			
		
		document.getElementById('sellout_record').addEventListener('tap', function() {
		    //打开卖出记录页
		    mui.openWindow({
					id: 'sell_record.html',
					url: 'sell_record.html',
					show: {
						autoShow:false
					},
					waiting:{
						autoShow:true,
						title:'正在加载...'
					}
				});
		})
		
		window.addEventListener('refresh_data',function(event){
		   refresh_data();
		});
		
		window.addEventListener('refresh_bank',function(event){
		   refresh_bank();
		});
		
		window.addEventListener('change_state',function(event){
		    country=event.detail.selcountry;
		    document.getElementById('country1').innerHTML=event.detail.selcountry;
		    refresh_data();
		    
		});
		
	});
    
    
    //加载数据
    function refresh_data(){
		userinfo = app.getState() || {};
        uid = userinfo.user_id;
        mui.ajax({
					url: apiurl + "Trade/sale_center/",
					data: {'p':1,'size':pageSize,'user_id':uid, 'country':country,'_token': userinfo.token},
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'get',
					timeout: 10000,
					success: function(res) {
						console.log(JSON.stringify(res));
						if(res.status == -3) {
							mui.toast(res.msg);
							app.out();
							return false;
						}
						// 请求成功
						if(!res.status) {
							mui.toast('未获取到数据');
						} else {
							
							$('#item1mobile ul').empty();
							$('#item2mobile ul').empty();
							$('#item3mobile ul').empty();
							$('#item4mobile ul').empty();
							
							
							var data_1=assemble_data(res.data.a500.resource);
							$('#item1mobile ul').append(data_1);
							//total_page1=res.total_page1;
						    //total_count1=res.sum1;
							var data_2=assemble_data(res.data.a1000.resource);
							$('#item2mobile ul').append(data_2);
							
							var data_3=assemble_data(res.data.a1500.resource);
							$('#item3mobile ul').append(data_3);
							
							var data_4=assemble_data(res.data.a2000.resource);
							$('#item4mobile ul').append(data_4);
							
                            total_page=[res.data.a500.total_page,res.data.a1000.total_page,res.data.a1500.total_page,res.data.a2000.total_page];
						    total_count=[res.data.a500.sum,res.data.a1000.sum,res.data.a1500.sum,res.data.a2000.sum];
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('获取数据失败！');
					}
				});
				
				//循环初始化所有下拉刷新，上拉加载。
				mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
						mui(pullRefreshEl).pullToRefresh({
							up: {
								callback: function() {
									var self = this;
									setTimeout(function() {
										currentPage[index]++;
										var ind=index+1;
										/*
										if(currentPage[index] > total_page[index] || total_count[index] <= $('#item'+ind+'mobile ul li').length) {
											self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
											return false;
										}*/
										if(currentPage[index] < total_page[index]) {
											self.endPullUpToRefresh(false);
										}
										else{
											self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
										}
										mui.ajax({
											url: apiurl + "Trade/sale_center/",
											data: {
												'p': currentPage[index],
												'size': pageSize,
												'user_id': uid,
												'money': num_arr[index],
												'country':country,
												'_token': userinfo.token
											},
											async: true,
											dataType: 'json',
											crossDomain: true, //强制使用5+跨域
											type: 'get',
											timeout: 10000,
											success: function(res) {
												console.log(JSON.stringify(res));
												if(res.status == -3) {
													mui.toast(res.msg);
													app.out();
													return false;
												}
												// 请求成功
												if(!res.status) {
													mui.toast('未获取到数据');
												} else {
													switch(num_arr[index])
													{
													case 500:
													    var data_1 = assemble_data(res.resource);
													    $('#item1mobile ul').append(data_1);
													    if(currentPage[index] < total_page[index]) {
													    	self.endPullUpToRefresh(false);
														}
													    else{
													    	self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
													    }
													    break;
													case 1000:
													    var data_1 = assemble_data(res.resource);
													    $('#item2mobile ul').append(data_1);
													    if(currentPage[index] < total_page[index]) {
															self.endPullUpToRefresh(false); 
														}
													    else{
													    	self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
													    }
													    break;
													case 1500:
													    var data_1 = assemble_data(res.resource);
													    $('#item3mobile ul').append(data_1);
													    if(currentPage[index] < total_page[index]) {
															self.endPullUpToRefresh(false); //参数为true代表没有更多数据了。
														}
													    else{
													    	self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
													    }
													    break;
													case 2000:
													    var data_1 = assemble_data(res.resource);
													    $('#item4mobile ul').append(data_1);
													    if(currentPage[index] < total_page[index]) {
															self.endPullUpToRefresh(false); //参数为true代表没有更多数据了。						
														}else{
													    	self.endPullUpToRefresh(true); //参数为true代表没有更多数据了。
													    }
													    break;
													}
													
												}
											},
											error: function(xhr, type, errorThrown) {
												//console.log(JSON.stringify(xhr));
												//console.log(JSON.stringify(type));
												//console.log(JSON.stringify(errorThrown));
												mui.toast('获取数据失败！');
											}
										});
										return ;
										
									}, 1500);
								}
						   }
						});
				});
    }
    
    
    function assemble_data(data){
    	/*
    	 * <li class="mui-table-view-cell">
										<div class="sellout_list">
											<img src="../../img/touxiang.png" alt="" />
											<div class="list_m">
												<span class="user_name">蜡笔小新</span>
												<span class="credit">信用<img src="../../img/love.png"/></span>
												<span class="payment_method">支付方式：招商银行</span>
											</div>
											<div class="list_r">
												<span class="sum">交易金额：500</span>
												<span class="amount">实付金额：425.00RMB</span>
												<span class="sale_btn">出售</span>
											</div>
										</div>
									</li>
    	 */
    	var htm='';
		for(z in data){
			htm+='<li class="mui-table-view-cell">';
			htm+='<div class="sellout_list">';
			if(data[z]['headimgurl'] && data[z]['headimgurl'].indexOf('Uploads') !== -1){
				//var imgpath=serverurl + 'Uploads/avatar/' + data[z]['buy_user_id'] + '.jpg';
				if(data[z]['headimgurl'].indexOf(upurl+'Uploads') !== -1){
					var imgpath=data[z]['headimgurl'];
				}
				else{
					var imgpath=upurl+data[z]['headimgurl'];
				}
			}
			else{
				var imgpath='../../img/logo.png';
			}
			htm += '<img src="'+imgpath+'" />';
			htm+='<div class="list_m">';
			htm+='<span class="user_name">'+data[z]['buy_contacts']+'</span>';
			htm+='<span class="credit">信用<img src="../../img/love.png"/></span>';
			htm+='<span class="payment_method">支付方式：'+data[z]['buy_bank_name']+'</span>';
			htm+='</div>';	
			
			htm+='<div class="list_r">';
			htm+='<span class="sum">交易金额：'+data[z]['all_money']+'</span>';
			htm+='<span class="amount">实付金额：'+data[z]['amount']+'</span>';
			htm+='<span class="sale_btn" data-tradeId="'+data[z]['id']+'">出售</span>';
			htm+='</div>';
			htm+='</div>';
			htm+='</li>';
		}
		return htm;
    }
	
	
	 function refresh_bank(){
		mui.ajax({
				url: apiurl + "Trade/get_def_bank/",
				data: {'user_id':userinfo.user_id,'_token': userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status == -3) {
						mui.toast(res.msg);
						app.out();
						return false;
					}
					
					// 请求成功
					userinfo.bank=res.data;
					app.setState(userinfo);
					bank=res.data;
                    
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
	}
				
			
				
				
					
					
				
			